﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Linq;
using System.Web;

/// <summary>
/// ASHX一般处理程序文件基类
/// </summary>
public abstract class AJAXHandlerBase : IHttpHandler
{
    #region 属性
    protected string UnKonwError = "未知错误！";

    /// <summary>
    /// HttpResponse对象
    /// </summary>
    protected HttpResponse Response;
    /// <summary>
    /// HttpRequest对象
    /// </summary>
    protected HttpRequest Request;
    /// <summary>
    /// 请求需调用方法的对应标识
    /// </summary>
    protected string action = string.Empty;

    #endregion

    #region IHttpHandler接口实现
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetLastModified(DateTime.Now);
        context.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));


        Response = context.Response;
        Request = context.Request;

        context.Response.Clear();

        action = Request["act"];
        AJAXHandler(action);
    }
    #endregion

    /// <summary>
    /// 处理AJAX请求方法
    /// </summary>
    /// <param name="action">请求动作标识</param>
    public abstract void AJAXHandler(string action);

    #region 调用Res/pponse.Write输出结果
    /// <summary>
    /// 调用Response.Write输出结果
    /// </summary>
    /// <param name="result"><aram>
    public void Write(object result)
    {
        Response.Write(result);
    }
    #endregion

    #region 输出成功提示，格式为JSON形式
    /// <summary>
    /// 输出成功提示，格式为JSON形式
    /// (例：{msg:"提示语",flag:1})
    /// </summary>
    /// <param name="Msg"></param>
    public void WriteSuccess(object Msg)
    {
        WriteJsonResult(Msg, 0);
    }

    /// <summary>
    /// 输出成功提示，格式为JSON形式
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="flag">输出结果标识</param>
    public void WriteJsonResult(object Msg, int flag)
    {
        string json = "{\"msg\":\"" + Msg.ToString() + "\",\"flag\":\"" + flag.ToString() + "\"}";
        Response.Write(json);
    }
    #endregion

    #region 输出错误提示，格式为JSON形式
    /// <summary>
    /// 输出成功提示，格式为JSON形式
    /// (例：{msg:"提示语",flag:0})
    /// </summary>
    /// <param name="Msg"></param>
    public void WriteError(object Msg)
    {
        WriteJsonResult(Msg, 1);
    }
    #endregion

    #region 将给定Request.Form集合赋值到给定Model
    /// <summary>
    /// 将给定Request.Form集合赋值到给定Model的对应属性上
    /// </summary>
    /// <param name="Form"></param>
    /// <param name="Model"></param>
    public static void SetModelByCollection(NameValueCollection Form, object Model)
    {
        var propertyArr = Model.GetType().GetProperties();
        foreach (PropertyInfo item in propertyArr)
        {
            if (Form[item.Name] != null)
            {
                item.SetValue(Model, Convert.ChangeType(Form[item.Name], Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType), null);
            }
        }
    }
    #endregion

}